package com.fenghongzhang.day019;

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.widget.RemoteViews;

import androidx.annotation.Nullable;

import java.io.IOException;
import java.util.List;

/**
 * 1,start
 * 2,bind
 */
public class MusicService extends Service {


    private MediaPlayer mediaPlayer ;
    private List<Music> list;

    private int index;

    public MusicService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();
        list = MusicUtil.getMusics(this);

        //动态注册广播接收者
        MyRervice myRervice = new MyRervice();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.feng.pause");
        registerReceiver(myRervice,intentFilter);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        //通知中可以上一首,下一首.
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);


        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);
        builder.setCustomContentView(remoteViews);
//        builder.setContent()//22
        Notification build = builder.build();

        startForeground(1,build);

        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MyBinder();
    }

    class MyBinder extends Binder{

        public MusicService getService(){
            return MusicService.this;
        }
//
//        public void callPlay(int position){
//            play(position);
//        }
//        public void callPause(){
//            pause();
//        }
//        public void callStop(){
//            stop();
//        }
//        public void callRestart(){
//            restart();
//        }
    }

    //在内容创建广播接收者
    class MyRervice extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if(action.equals("com.feng.pause")){
                pause();
            }
        }
    }

    public void play(int position){

        //更新通知
        //通知中可以上一首,下一首.
        Notification.Builder builder = new Notification.Builder(this);
        builder.setSmallIcon(R.mipmap.ic_launcher);

        RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notify_layout);
        //设置歌名
        remoteViews.setTextViewText(R.id.notify_title,list.get(position).getTitle());

        //点击暂停产生的.
        Intent intent = new Intent();
        intent.setAction("com.feng.pause");
        PendingIntent broadcast = PendingIntent.getBroadcast(this, 0, intent, 0);
        remoteViews.setOnClickPendingIntent(R.id.notify_pause,broadcast);



        builder.setCustomContentView(remoteViews);

//        builder.setContent()//22
        Notification build = builder.build();

        startForeground(1,build);

        //-------

        index = position;
        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(list.get(position).getData());
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public void seetTo(int position){
        mediaPlayer.seekTo(position);
    }

    public void pause(){
        mediaPlayer.pause();
    }

    public int getPosition(){
        return mediaPlayer.getCurrentPosition();
    }

    public int getMax(){
        return mediaPlayer.getDuration();
    }
    public void stop(){
        mediaPlayer.stop();
    }
    public void restart(){
        mediaPlayer.start();
    }

    public void next(){
        index++;
        if(index >= list.size() -1){
            index = 0;
        }
        play(index);
    }

}
